/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2014 All Rights Reserved.
 */
package com.alipay.marketing;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayMarketingCardOpenRequest;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipayMarketingCardOpenResponse;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.alipay.constants.AlipayServiceEnvConstants;
import com.alipay.factory.AlipayAPIClientFactory;
import com.alipay.util.RequestUtil;

import net.sf.json.JSONObject;

/**
 * 会员开卡，需要获取用户授权
 * 
 * @author zwm
 */
public class OpenCardServlet extends HttpServlet {

	/**  */
	private static final long serialVersionUID = -6807693807426739985L;
	
	static String  USER_INFO_SHARE_RESPONSE_OBJECT = "alipay_user_info_share_response";
	/**
	 * 业务处理
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1. 解析请求参数
		Map<String, String> params = RequestUtil.getRequestParams(request);
		// 2. 获得authCode
		String authCode = params.get("auth_code");
		System.out.println(authCode);
		try {
			AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();
			AlipaySystemOauthTokenRequest oauthTokenRequest = new AlipaySystemOauthTokenRequest();
			oauthTokenRequest.setCode(authCode);
			oauthTokenRequest.setGrantType(AlipayServiceEnvConstants.GRANT_TYPE);

			AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(oauthTokenRequest);
			System.out.println(oauthTokenResponse.getAccessToken());
			String accessToken = oauthTokenResponse.getAccessToken();

			AlipayMarketingCardOpenRequest cardRequest = new AlipayMarketingCardOpenRequest();
			// request.putOtherTextParam("app_auth_token", appAuthToken);
			cardRequest.putOtherTextParam("app_auth_token", null);

			String bizContent = "{" + "\"out_serial_no\":\"2017102010482088154\","// 唯一标识
					+ "\"card_template_id\":\"20170206000000000130129000300905\","// 会员卡模板id，通过模板创建接口返回得到
					+ "\"card_user_info\":{" + "\"user_uni_id\":\"2088702468804908\"," // 支付宝userid(一般2088开头)
					+ "\"user_uni_id_type\":\"UID\"}," 
					+ "\"card_ext_info\":{"
					+ "\"external_card_no\":\"EXT0009\","// 商户会员卡卡号
					+ "\"open_date\":\"2018-10-08 14:20:00\","
					+ "\"valid_date\":\"2020-03-20 21:20:46\","
					+ "\"level\":\"VIP1\","
					+ "\"point\":\"88\","
					+ "\"balance\":\"134.89\"},"
					+ "\"member_ext_info\":{"
					+ "\"name\":\"张伟明\","
					+ "\"gende\":\"FEMALE\","
					+ "\"birth\":\"2016-06-27\","
					+ "\"cell\":\"13419657975\"}}";

			cardRequest.setBizContent(bizContent);
			AlipayMarketingCardOpenResponse cardResponse = alipayClient.execute(cardRequest, accessToken);
			System.out.println(cardResponse.getBody());
			
		} catch (AlipayApiException alipayApiException) {
			// 自行处理异常
			alipayApiException.printStackTrace();
		}
	}
}
